//
//  BZYSortController.m
//  BZYMeituan
//
//  Created by 毕仲阳 on 16/9/17.
//  Copyright © 2016年 mycompany. All rights reserved.
//

#import "BZYSortController.h"
#import "BZYSortModel.h"
@interface BZYSortController ()
@property (nonatomic,strong) NSArray *sortArray;
@end

@implementation BZYSortController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    NSArray *sortList = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"sorts.plist" ofType:nil]];
    self.sortArray = [NSArray yy_modelArrayWithClass:[BZYSortModel class] json:sortList];
    
    CGFloat width = 100;
    CGFloat height = 30;
    CGFloat margin = 15;

    NSInteger count = self.sortArray.count;
    for (int i = 0; i < count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        BZYSortModel *sortModel = self.sortArray[i];
        
        [button setTitle:sortModel.label forState:UIControlStateNormal];
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        
        [button setBackgroundImage:[UIImage imageNamed:@"btn_filter_normal"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"btn_filter_selected"] forState:UIControlStateHighlighted];
        
        //设置 frame
        button.width = width;
        button.height = height;
        button.x = margin;
        button.y = margin + (button.height + margin) * i;
        
        button.tag = i;
        [button addTarget:self action:@selector(sortButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
    }
    //设置 popover 大小
    CGFloat contentWidth = 2 * margin + width;
    CGFloat contentHeight = (margin + height) * count + margin;
    self.preferredContentSize = CGSizeMake(contentWidth, contentHeight);

}

- (void)sortButtonClick:(UIButton *)button
{
    [[NSNotificationCenter defaultCenter] postNotificationName:BZYSortDidChangeNotification object:nil userInfo:@{BZYSortModelKey : self.sortArray[button.tag]}];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
